using System.Reflection;

namespace UniversalAdminSystem.Domian;

public class DomainServices
{
    public static IEnumerable<(Type ServiceType, Type ImplementationType)> DiscoverServices()
    {
        var domainAssembly = Assembly.GetExecutingAssembly();
        var serviceInterfaces = domainAssembly.GetTypes()
            .Where(x => x.IsInterface && x.Name.EndsWith("DomainService"));
        var serviceImplementations = domainAssembly.GetTypes()
            .Where(x => x.IsClass && !x.IsAbstract && x.Name.EndsWith("DomainService"));

        foreach (var iface in serviceInterfaces)
        {
            var impl = serviceImplementations.FirstOrDefault(x => iface.IsAssignableFrom(x));
            if (impl != null)
            {
                yield return (iface, impl);
            }
        }
    }
}